/*
NOTES TO DATA ANALYSIS:
	1. Treatment effect regressions for spillover outcomes: respiratory hygiene and wearing masks by pooled treatments
*/

clear all
set more off
capture log close

********************************************************************************

use "$covidclean/smscovid_clean.dta", clear

***Keep relevant observations: consent taken and above 18 years of age, non-missing treatment arms
	keep if consent==1 & age>=18
	keep if ~missing(treatment_arm)
	count //3964

***HOUSEKEEPING - BEHAVIOR TREATMENTS, OUTCOMES, ROW TITLES

	***Create dummies with treatment arms interacted with behavior
		foreach var in $treat_pool $treat_frames $treat_timings $treat_arms {
			gen `var'_sd = `var'*behavior_sd
			gen `var'_hw = `var'*behavior_hw
		}
		
	***Outcomes		
		global spillover know_mask act_mask mask_1 know_cover act_cover cover_1
		
	***Row titles
		global rtitle_pooled "Treatment - SD \ "" \ "" \ Treatment - HW \ "" \ "" "

********************************************************************************
***ITT REGRESSIONS - SPILLOVERS - POOLED TREATMENT
********************************************************************************
	
		qui foreach v of varlist $spillover {			
				
				*Control mean
				mean `v' if treatment_pooled==0 & $sample
				mat mean = r(table)
				local c_mean : di %9.2f mean[1,1]

				*Regression
				reghdfe `v' $treat_pool_sd $treat_pool_hw if $sample, a($studycontrols $covariates) vce(robust)
				
				mat b = r(table)
				local tcount: word count $treat_pool_sd $treat_pool_hw				
				forval i = 1/`tcount' {
					local b_`i' = b[1,`i'] //beta
					local se_`i' = b[2,`i'] //asymptotic s.e.
					local a_p_`i' = b[4,`i'] //asymptotic p-value
					local p_`i' = 0 //exact p-value
				}
				
				local n : di %9.0fc e(N)
				local r2_a : di %9.2f e(r2_a)
				
				*Randomization inference using RI dataset created
				preserve
    				forval batch = 1/$nbatch {
					   use if batch == `batch' using "$rianalysis/ri_data_final", clear
    				   noi di "Running ITT-pooled for `v' for batch - `batch' at time: `c(current_time)'"
    				   reghdfe `v' $treat_pool_sd $treat_pool_hw if $sample, a($studycontrols $covariates) vce(robust) 
    				   mat ri_b = r(table)
    				   forval i = 1/`tcount' {
            			   local ri_b_`i' = ri_b[1,`i']
            			   *One-tailed comparison
				           if `ri_b_`i'' >= `b_`i'' local p_`i' = `p_`i'' + 1 
					    }  
				    } 
				restore
				
				*Compute Exact p-values 
				forval i = 1/`tcount' {
					local p_`i' = `p_`i''/$nbatch
				}
				
				*Imputing coefficients, asymptotic and exact p-values in the output matrix
				mat `v'_p = J(`tcount',3,.)
				forval i = 1/`tcount' {
					mat `v'_p[`i',1] = round(`b_`i'',0.001)
					mat `v'_p[`i',2] = round(`se_`i'',0.001)
					mat `v'_p[`i',3] = round(`p_`i'',0.001)
				}
				
				
				*Statistical significance
				mat stars = J(`tcount',3,.)
				forval i = 1/`tcount' {
					mat stars[`i',2] = 0
					mat stars[`i',3] = 0
					if (`a_p_`i'' < 0.1)  mat stars[`i',1] = 1
					else mat stars[`i',1] = 0
					if (`a_p_`i'' < 0.05) mat stars[`i',1] = 2 
					if (`a_p_`i'' < 0.01) mat stars[`i',1] = 3 
				}
				
				*Save matrix
				matsave `v'_p, replace saving path($tables)
				*matload `v'_p, path($tables) saving
				
				*Output 
				frmttable, statmat(`v'_p) store(`v'_p) substat(2) annotate(stars) asymbol(*,**,***) rtitle($rtitle_pooled) /// 
				addrows("Adjusted $ R^2$", `r2_a' \ N, "`n'" \ "Control Mean", `c_mean') sdec(3,3,3)
								
		}
		
********************************************************************************
***COMPILE OUTPUTS
********************************************************************************

	qui foreach v of varlist $spillover {	
		outreg, replay(pooled_spill) merge(`v'_p) store(pooled_spill) 
	}

	***Globals for outputs
	global font "basefont(scriptsize) statfont(scriptsize; scriptsize; scriptsize; scriptsize) rtitlfont(scriptsize; scriptsize; scriptsize; scriptsize) ctitlfont(scriptsize) notefont(scriptsize)"
	global lines "hlines(1{0};1{0};1{0}1;1{0}1)"
		
	***Display all results
	cd "$tables"
	outreg using itt_spill, replace tex fr replay(pooled_spill) $font note("") $lines ///
			ct("", "Wearing Masks", "", "", "Respiratory Hygiene", "", "" \ "", "Know", "Act", "Direct", "Know", "Act", "Direct") multicol(1,2,3;1,5,3)			

exit, clear
